package com.nidu.demo.user.executor.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.Response;
import com.nidu.demo.user.ability.UserDomainService;
import com.nidu.demo.user.gateway.UserDepartmentGateway;
import com.nidu.demo.user.gateway.UserGateway;
import com.nidu.demo.user.gateway.UserPositionGateway;
import com.nidu.demo.user.gateway.UserRoleGateway;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Component
@RequiredArgsConstructor
public class UserDeleteCmdExe {

    private final UserGateway userGateway;

    private final UserDepartmentGateway userDeptGateway;

    private final UserPositionGateway userPostGateway;

    private final UserRoleGateway userRoleGateway;

    public Response execute(Long id) {
        if(id != null){
            this.deleteUser(id);
        }
        return Response.buildSuccess();
    }

    public Response execute(Set<Long> ids){
        if(!CollUtil.isEmpty(ids)){
            this.deleteUsers(ids);
        }
        return Response.buildSuccess();
    }

    @Transactional
    public void deleteUser(Long userId){
        userRoleGateway.removeUserRoles(userId);

        userDeptGateway.removeUserDepts(userId);

        userPostGateway.removeUserPosts(userId);

        userGateway.deleteById(userId);
    }

    @Transactional
    public void deleteUsers(Set<Long> userIds){

        userRoleGateway.removeUsersRoles(userIds);

        userDeptGateway.removeUsersDepts(userIds);

        userPostGateway.removeUsersPosts(userIds);

        userGateway.deleteByIds(userIds);
    }
}
